package com.example.demo;

import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@SpringBootApplication
@Controller
public class Demo4Application {
	@Autowired
	private IUserService userService;
	public static void main(String[] args) {
		SpringApplication.run(Demo4Application.class, args);
	}

	@RequestMapping("/")
	public ModelAndView index(Model model) {
		model.addAttribute("users",null);
		return new ModelAndView("query", "model", model);

	}
	@RequestMapping("/query")
	public ModelAndView query(Model model,User user) {
		model.addAttribute("users", userService.findByNameLike("%"+user.getName()+"%"));
		return new ModelAndView("query", "model", model);
	}

	@RequestMapping("/add")
	public ModelAndView add(Model model,User user) {
		userService.save(user);
		model.addAttribute("users", userService.findAll());
		return new ModelAndView("query", "model", model);
	}
}
